<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        ul {
            list-style: none;
        }

        ol {
            list-style: none;
        }

        .container {
            position: relative;
            width: 550px;
            height: 242px;
            background-color: #ccc;
            margin: 0 auto;
            overflow: hidden;
        }

        .container ul {
            position: absolute;
        }

        .container ul li {
            float: left;
            width: 550px;
            height: 242px;
        }

        .container ul li a {
            position: relative;
            display: block;
        }

        .container ul li a img {
            width: 100%;
            height: 100%;
        }

        .container .arrow-left {
            user-select: none;
            display: none;
            cursor: pointer;
            position: absolute;
            left: 0;
            top: 50%;
            height: 50%;
            transform: translateY(-50%);
            width: 60px;
            background: linear-gradient(to right, rgba(0, 0, 0, 0.513), rgba(51, 51, 51, 0));
            /* background-color: black; */
            z-index: 1;
            text-align: center;
            line-height: 121px;
            font-size: 40px;
            font-weight: bolder;
            color: rgba(255, 255, 255, 0.746);
        }

        .container .arrow-right {
            user-select: none;
            display: none;
            cursor: pointer;
            position: absolute;
            right: 0;
            top: 50%;
            height: 50%;
            width: 60px;
            transform: translateY(-50%);
            background: linear-gradient(to left, rgba(0, 0, 0, 0.513), rgba(51, 51, 51, 0));
            z-index: 1;
            text-align: center;
            line-height: 121px;
            font-size: 40px;
            font-weight: bolder;
            color: rgba(255, 255, 255, 0.746);
        }

        .container ol {
            box-sizing: border-box;
            position: absolute;
            padding-top: 9px;
            padding-left: 7px;
            /* width: 80%; */
            right: 0;
            height: 32px;
            transform: translateX(-10%);
            bottom: 10px;
            z-index: 2;
        }

        .container ol li {
            position: relative;
            cursor: pointer;
            float: left;
            width: 10px;
            height: 10px;
            border: 2px solid #fff;
            margin-right: 7px;
            border-radius: 50%;
            transition: all .2s;
            /* transform-origin: center; */
        }

        .container ul li a::before {
            content: '';
            position: absolute;
            left: 0;
            bottom: 0;
            display: block;
            width: 100%;
            height: 60px;
            /* z-index: 0; */
            background: linear-gradient(to top, rgba(0, 0, 0, 0.513), rgba(51, 51, 51, 0));

        }

        .container ul li a p {
            position: absolute;
            width: 60%;
            left: 0;
            bottom: 12px;
            padding: 3px 10px;
            color: #fff;
            text-shadow: 0px 0px 10px #000000;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
        }

        .container ul li a .bsad {
            display: inline-block;
            vertical-align: -6px;
            width: 38px;
            height: 22px;
            background: url("./img/bsad.png");
            background-size: 38px auto;
            margin-right: -2px;
        }

        .container ol li:hover {
            /* margin-right: 8px; */
            width: 12px;
            height: 12px;
            margin-top: -1px;
            background-color: skyblue;
        }

        .container ol li.current {
            width: 18px;
            height: 18px;
            transform: scale(1.3);
            background: url("./img/icon_slide_selected.png") no-repeat center;
            background-size: cover;
            margin-top: -4px;
            background-color: transparent;
            border-color: transparent;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="arrow-left"> &lt; </div>
        <div class="arrow-right"> &gt; </div>
        <ol>
        </ol>
        <ul>
            <li>
                <a href="javascript:;">
                    <img src="./img/1.jpg" alt="">
                    <p class="title">
                        <span class="bsad"></span>
                        钢铁侠继续出击！去寻找更强大的敌人并将他们碾碎
                    </p>
                </a>
            </li>
            <li>
                <a href="javascript:;">
                    <img src="./img/2.jpg" alt="">
                    <p class="title">
                        公主连结,今日公测,你在等什么
                    </p>
                </a>
            </li>
            <li>
                <a href="javascript:;">
                    <img src="./img/3.jpg" alt="">
                    <p class="title">
                        <span class="bsad"></span>
                        钢铁侠继续出击！去寻找更强大的敌人并将他们碾碎
                    </p>
                </a>
            </li>
            <li>
                <a href="javascript:;">
                    <img src="./img/4.jpg" alt="">
                    <p class="title">
                        <span class="bsad"></span>
                        钢铁侠继续出击！去寻找更强大的敌人并将他们碾碎
                    </p>
                </a>
            </li>
            <li>
                <a href="javascript:;">
                    <img src="./img/5.jpg" alt="">
                    <p class="title">
                        <span class="bsad"></span>
                        钢铁侠继续出击！去寻找更强大的敌人并将他们碾碎
                    </p>
                </a>
            </li>
        </ul>
    </div>
</body>
<script src="./js/animate-lunbo.js"></script>
<script>
    var li_length = document.querySelectorAll('.container ul li').length;
    var ul = document.querySelector(".container ul");
    var container = document.querySelector(".container");
    var arrow_left = document.querySelector(".container .arrow-left");
    var arrow_right = document.querySelector(".container .arrow-right");
    var ol = document.querySelector(".container ol");
    var timer = null;
    //初始化ul

    //初始化左右箭头的出现与消失
    container.addEventListener('mouseenter', function () {
        console.log('清除定时器');
        clearInterval(timer);
        arrow_left.style.display = 'block';
        arrow_right.style.display = 'block';
    });
    container.addEventListener('mouseleave', function () {
        console.log('添加定时器');
        timer = setInterval(function () {
            arrow_right.click();
        }, 2000);
        arrow_left.style.display = 'none';
        arrow_right.style.display = 'none';
    });
    //初始化左右箭头的出现与消失

    var firstClone = ul.firstElementChild.cloneNode(true);
    ul.appendChild(firstClone);
    ul.style.width = `${ul.childNodes.length * 100}%`;

    var index = 0;
    var pointIndex = 0;
    //生成li
    //并为每个li绑定事件
    for (let i = 0; i < li_length; i++) {
        let li = document.createElement('li');
        ol.appendChild(li);
        ol.children[i].setAttribute('index', i);
        ol.children[i].addEventListener('click', function () {
            //干掉所有人
            for (let i = 0; i < li_length; i++) {
                ol.children[i].classList.remove('current');
            }
            //留下我自己
            this.classList.add('current');
            //获取当前索引
            var thisIndex = this.getAttribute('index');
            index = thisIndex;
            pointIndex = thisIndex;
            console.log(index);
            animate_lunbo(ul, -thisIndex * container.offsetWidth);
        });
    }

    //为第一个li初始化current class类名
    ol.firstElementChild.classList.add('current');
    arrow_right.addEventListener('click', function () {
        if (index == ul.children.length - 1) {
            index = 0;
            ul.style.left = 0;
        }
        console.log(index);
        index++;
        pointIndex++;
        killLiClass();
        if (pointIndex == 5) {
            pointIndex = 0;
        }
        ol.children[pointIndex].classList.add('current');
        animate_lunbo(ul, -index * container.offsetWidth, function () {
            console.log('ok');
        });
    });
    arrow_left.addEventListener('click', function () {
        if (index <= 0) {
            console.log('index已更改为5');
            index = ul.children.length - 1;
        }
        if (pointIndex <= 0) {
            console.log('pointIndex更改为4');
            pointIndex = 5;
        }
        console.log(index);
        index--;
        pointIndex--;
        console.log('pointIndex'+pointIndex);
        killLiClass();
        ol.children[pointIndex].classList.add('current');
        animate_lunbo(ul, -index * container.offsetWidth, function () {
            console.log('ok');
        });
    });

    function killLiClass() {
        for (let i = 0; i < li_length; i++) {
            ol.children[i].classList.remove('current');
        }
    }

    timer = setInterval(function () {
        console.log(pointIndex);
        arrow_right.click();
    }, 2000);
</script>

</html>